/* Decompiler 69ms, total 341ms, lines 627 */
import java.util.Scanner;

public class BookManager {
   static Scanner sc;
   static String[][] users;
   static String[][] books;
   static String[][] pubs;
   static String loginUser;

   public static void main(String[] args) {
      init();
      index();
   }

   private static void index() {
      while(true) {
         System.out.println("欢迎来到龙院图书管理系统！");
         System.out.println("1.登录 2.注册");
         int logOrReg = sc.nextInt();
         switch(logOrReg) {
         case 1:
            login();
            break;
         case 2:
            register();
            break;
         default:
            System.out.println("系统退出！");
            System.exit(0);
         }
      }
   }

   private static void register() {
      System.out.println("请输入所属部门：");
      String deptName = sc.next();
      System.out.println("请输入用户名：");
      String userName = sc.next();
      System.out.println("请输入密码：");
      String passWord = sc.next();

      for(int i = 0; i < users.length; i++) {
         if (users[i][1] == null) {
            users[i][0] = deptName;
            users[i][1] = userName;
            users[i][2] = passWord;
            users[i][3] = "普通用户";
            System.out.println("注册成功！");
            break;
         }
      }

   }

   private static boolean login() {
      boolean logFlag = false;

      while(true) {
         String userName;
         String passWord;
         do {
            do {
               if (logFlag) {
                  return false;
               }

               System.out.println("请输入用户名：");
               userName = sc.next();
               System.out.println("请输入密码：");
               passWord = sc.next();
            } while(StringUtil.isEmpty(userName));
         } while(StringUtil.isEmpty(passWord));

         for(int i = 0; i < users.length; i++) {
            if (userName.equals(users[i][1]) && passWord.equals(users[i][2])) {
               System.out.println("登录成功！");
               loginUser = userName;
               homePage();
            }
         }

         System.out.println("输入错误，请重新输入！");
      }
   }

   private static void homePage() {
      System.out.println(loginUser + "， 欢迎您使用龙院书籍管理系统！！！");

      while(true) {
         while(true) {
            System.out.println("请输入数字进行选择：1 图书管理  2 出版社管理  3 退出登录  4 退出系统");
            int key = sc.nextInt();
            if (key == 1){
               bookManage();
            }else if (key ==2){
               pubManage();
            }else if (key ==3){
               loginUser = "";
               System.out.println("已退出登录！");
               index();
            }else if (key ==4){
               System.out.println("系统退出成功！");
               System.exit(0);
            }else{
            System.out.println("输入错误，请重新输入！");
            }
         }
      }
   }

   private static void pubManage() {
      while (true) {
         System.out.println("请输入：1.增加 2.删除 3.更新 4.根据出版社名称查询 5.查询所有出版社 6.返回上一级菜单");
         int key = sc.nextInt();
         if (key == 1) {
            addPub();
         } else if (key == 2) {
            delPub();
         } else if (key == 3) {
            updatePub();
         } else if (key == 4) {
            searchPubByName();
         } else if (key == 5) {
            searchAllPubCom();
         } else if (key ==6){
            homePage();
         }
      }
   }

   static void searchAllPubCom() {
      System.out.println("出版社名称\t地址 \t联系人\t");

      for(int i = 0; i < pubs.length; i++) {
         if (pubs[i][0] != null) {
            System.out.println(pubs[i][0] + "\t" + pubs[i][1] + "\t" + pubs[i][2] + "\t");
         }
      }

   }

   private static void searchPubByName() {
      System.out.println("请输入出版社名称：");
      String pcName = sc.next();
      int index = getPubIndex(pcName);
      if (index != -1) {
         System.out.println("出版社名称：" + pubs[index][0]);
         System.out.println("出版社地址：" + pubs[index][1]);
         System.out.println("出版社联系人：" + pubs[index][2]);
      } else {
         System.out.println("该出版社不存在！！！");
      }

   }

   private static void updatePub() {
      System.out.println("请输入要更新的出版社名称：");
      String pubName = sc.next();
      int index = getPubIndex(pubName);
      printPubCom(index);
      System.out.println("请输入要更新的地址：");
      String pcAddress = sc.next();
      System.out.println("请输入要更新的联系人姓名：");
      String pcPerson = sc.next();
      boolean b = updatePubCom(index, pcAddress, pcPerson);
      if (b) {
         System.out.println("更新成功");
         printPubCom(index);
      } else {
         System.out.println("更新失败！！！");
      }

   }

   static boolean updatePubCom(int index, String pcAddress, String pcPerson) {
      boolean b = false;
      pubs[index][1] = pcAddress;
      pubs[index][2] = pcPerson;
      b = true;
      return b;
   }

   private static void delPub() {
      System.out.println("请输入要删除的出版社名称：");
      String pubName = sc.next();
      boolean b = delByPCName(pubName);
      if (b) {
         System.out.println("删除成功");
      } else {
         System.out.println("删除失败");
      }

   }

   private static void addPub() {
      System.out.println("请输入出版社名称：");
      String pcName = sc.next();
      System.out.println("请输入出版社地址：");
      String pcAddress = sc.next();
      System.out.println("请输入出版社联系人：");
      String pcPerson = sc.next();
      boolean b = insertPC(pcName, pcAddress, pcPerson);
      if (b) {
         System.out.println("出版社添加成功");
      } else {
         System.out.println("出版社添加失败");
      }

   }

   static void printPubCom(int index) {
      System.out.println("出版社名称\t地址\t联系人\t");
      System.out.print(pubs[index][0] + "\t");
      System.out.print(pubs[index][1] + "\t");
      System.out.println(pubs[index][2] + "\t");
   }

   static boolean insertPC(String pcName, String pcAddress, String pcPerson) {
      boolean b = false;
      if (getPubIndex(pcName) == -1) {
         int index = getPCIndexOfFirstNull();
         pubs[index][0] = pcName;
         pubs[index][1] = pcAddress;
         pubs[index][2] = pcPerson;
         b = true;
      }

      return b;
   }

   static int getPCIndexOfFirstNull() {
      int index = -1;

      for(int i = 0; i < pubs.length; i++) {
         if (pubs[i][0] == null) {
            index = i;
            break;
         }
      }

      return index;
   }

   private static void bookManage() {
      while(true) {
         System.out.println("请输入：1.增加 2.删除 3.更新 4.查询 5.返回上一级菜单");
         int key = sc.nextInt();
         if (key == 1) {
            addBook();
         } else if (key == 2) {
            delBook();
         } else if (key == 3) {
            updateBook();
         } else if (key == 4) {
            searchBook();
         } else {
            homePage();
         }
      }
   }

   static void searchBookByISBN(String isbn) {
      System.out.println("ISBN\t书名\t价格\t出版社\t作者\t");

      for(int i = 0; i < books.length; i++) {
         if (isbn.equals(books[i][0])) {
            System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
            break;
         }
      }

   }

   private static void searchBook() {
      boolean y = true;

      while(true) {
         while(y) {
            System.out.println("请输入查询种类：1.isbn 2.书名（模糊） 3.出版社 4. 作者 5. 价格范围 6.查询所有 7.返回上一级 ");
            int h = sc.nextInt();
            String author;
            if (h == 1) {
               System.out.println("请输入ISBN号：");
               author = sc.next();
               searchBookByISBN(author);
            } else if (h == 2) {
               System.out.println("请输入书名：");
               author = sc.next();
               searchByBookName(author);
            } else if (h == 3) {
               System.out.print("请输入出版社前的数字进行选择：");
               printPC();
               int d = sc.nextInt();
               if (d >= 1 && d <= getNumOfPC()) {
                  String pcName = pubs[d - 1][0];
                  searchBookByPC(pcName);
               } else {
                  System.out.println("请选择正确的数字！！！");
               }
            } else if (h == 4) {
               System.out.println("请输入作者姓名：");
               author = sc.next();
               searchBookByAuthor(author);
            } else if (h == 5) {
               System.out.println("请输入最低价格");
               double min = sc.nextDouble();
               System.out.println("请输入最高价格");
               double max = sc.nextDouble();
               searchBookByPrice(min, max);
            } else if (h == 6) {
               searchAllBook();
            } else if (h == 7) {
               y = false;
            } else {
               System.out.println("请输入正确的数字选项！！！");
            }
         }

         return;
      }
   }

   static void searchAllBook() {
      System.out.println("ISBN\t书名\t价格\t出版社\t作者\t");

      for(int i = 0; i < books.length; i++) {
         if (books[i][0] != null) {
            System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
         }
      }

   }

   static void searchBookByPrice(double min, double max) {
      System.out.println("ISBN\t书名\t价格\t出版社\t作者\t");

      for(int i = 0; i < books.length; i++) {
         if (books[i][0] != null) {
            double price = Double.parseDouble(books[i][2]);
            if (price >= min && price <= max) {
               System.out.println(books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
            }
         }
      }

   }

   static void searchBookByAuthor(String author) {
      System.out.println("\tISBN\t书名\t价格\t出版社\t作者\t");

      for(int i = 0; i < books.length; i++) {
         if (author.equals(books[i][4])) {
            System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
         }
      }

   }

   static void searchBookByPC(String pcName) {
      System.out.println("\tISBN\t书名\t价格\t出版社\t作者\t");

      for(int i = 0; i < books.length; i++) {
         if (pcName.equals(books[i][3])) {
            System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
         }
      }

   }

   static int getNumOfPC() {
      int count = 0;

      for(int i = 0; i < pubs.length; i++) {
         if (pubs[i][0] != null) {
            ++count;
         }
      }

      return count;
   }

   static void printPC() {
      for(int i = 0; i < pubs.length; i++) {
         if (pubs[i][0] != null) {
            System.out.print(i + 1 + ". " + pubs[i][0] + " ");
         }
      }

      System.out.println();
   }

   static void searchByBookName(String bookName) {
      System.out.println("\tISBN\t书名\t价格\t出版社 \t作者\t");

      for(int i = 0; i < books.length; i++) {
         if (books[i][0] != null && books[i][1].indexOf(bookName) != -1) {
            System.out.println("\t" + books[i][0] + "\t" + books[i][1] + "\t" + books[i][2] + "\t" + books[i][3] + "\t" + books[i][4] + "\t");
         }
      }

   }

   private static void updateBook() {
      System.out.println("请输入ISBN号：");
      String isbn = sc.next();
      int index = getIndexByISBN(isbn);
      if (index != -1) {
         System.out.println("请输入新的书名：");
         String bookName = sc.next();
         System.out.println("请输入新的价格：");
         String bookPrice = sc.next();
         System.out.println("请输入新的出版社：");
         String pubCom = sc.next();
         System.out.println("请输入新的作者：");
         String author = sc.next();
         boolean b = updateBook(index, bookName, bookPrice, pubCom, author);
         if (b) {
            System.out.println("更新成功！！！");
         } else {
            System.out.println("更新失败");
         }
      } else {
         System.out.println("该ISBN号不存在！！！");
      }

   }

   static boolean updateBook(int index, String bookName, String bookPrice, String pubCom, String author) {
      boolean b = false;
      books[index][1] = bookName;
      books[index][2] = bookPrice;
      books[index][3] = pubCom;
      books[index][4] = author;
      b = true;
      return b;
   }

   private static void delBook() {
      System.out.println("请输入要删除的出版社名称：");
      String pcName = sc.next();
      boolean b = delByPCName(pcName);
      if (b) {
         System.out.println("删除成功");
      } else {
         System.out.println("删除失败");
      }

   }

   static int getPubIndex(String pcName) {
      int index = -1;

      for(int i = 0; i < pubs.length; i++) {
         if (pcName.equals(pubs[i][0])) {
            index = i;
            break;
         }
      }

      return index;
   }

   static boolean isExistInBook(String pcName) {
      boolean bool = false;

      for(int i = 0; i < books.length; i++) {
         if (pcName.equals(books[i][3])) {
            bool = true;
            break;
         }
      }

      return bool;
   }

   static boolean delByPCName(String pcName) {
      boolean b = false;
      int index = getPubIndex(pcName);
      if (index != -1) {
         if (!isExistInBook(pcName)) {
            for(int i = index; i < pubs.length - 1; i++) {
               pubs[i] = pubs[i + 1];
            }

            b = true;
         } else {
            System.out.println("该出版社有相关图书信息存在！不可以删除！如果要删除请先删除所有相关图书信息！！！");
         }
      }

      return b;
   }

   private static void addBook() {
      System.out.println("请输入图书ISBN:");
      String isbn = sc.next();
      System.out.println("请输入书名:");
      String bookName = sc.next();
      System.out.println("请输入价格:");
      String bookPrice = sc.next();
      System.out.println("请输入出版社:");
      String pubCom = sc.next();
      System.out.println("请输入作者:");
      String author = sc.next();
      boolean b = insertBook(isbn, bookName, bookPrice, pubCom, author);
      if (b) {
         System.out.println("添加成功！！！");
      } else {
         System.out.println("添加失败！！！该书已经存在！！！");
      }

   }

   static boolean insertBook(String isbn, String bookName, String bookPrice, String pubCom, String author) {
      boolean b = false;
      int i = getIndexByISBN(isbn);
      if (i == -1) {
         int index = getBookIndexOfFirstNull();
         books[index][0] = isbn;
         books[index][1] = bookName;
         books[index][2] = bookPrice;
         books[index][3] = pubCom;
         books[index][4] = author;
         b = true;
      }

      return b;
   }

   static int getIndexByISBN(String isbn) {
      int index = -1;

      for(int i = 0; i < books.length; i++) {
         if (isbn.equals(books[i][0])) {
            index = i;
            break;
         }
      }

      return index;
   }

   static int getBookIndexOfFirstNull() {
      int index = -1;

      for(int i = 0; i < books.length; i++) {
         if (books[i][0] == null) {
            index = i;
            break;
         }
      }

      return index;
   }

   private static void init() {
      books[0][0] = "97871010";
      books[0][1] = "史记";
      books[0][2] = "125.0";
      books[0][3] = "中华书局";
      books[0][4] = "司马迁";
      books[1][0] = "97875063";
      books[1][1] = "活着";
      books[1][2] = "20.0";
      books[1][3] = "作家出版社";
      books[1][4] = "余华";
      books[2][0] = "97872290";
      books[2][1] = "三体全集";
      books[2][2] = "168.0";
      books[2][3] = "重庆出版社";
      books[2][4] = "刘慈欣";
      books[3][0] = "97875442";
      books[3][1] = "白夜行";
      books[3][2] = "39.5";
      books[3][3] = "南海出版公司";
      books[3][4] = "[日]东野圭吾";
      pubs[0][0] = "中华书局";
      pubs[0][1] = "北京市王府井大街36号";
      pubs[0][2] = "张三";
      pubs[1][0] = "作家出版社";
      pubs[1][1] = "香港九龙荷李活商业中心8楼";
      pubs[1][2] = "李四";
      pubs[2][0] = "重庆出版社";
      pubs[2][1] = "茶园新区开拓路6号";
      pubs[2][2] = "王五";
      pubs[3][0] = "南海出版公司";
      pubs[3][1] = "海秀中路51-1号星华大厦5楼";
      pubs[3][2] = "周毅";
   }

   static {
      sc = new Scanner(System.in);
      users = new String[20][4];
      books = new String[20][5];
      pubs = new String[20][3];
      loginUser = "";
   }
}
